package com.zhiyou100.crm.dao.impl;

import com.zhiyou100.crm.dao.RoleDao;
import com.zhiyou100.crm.util.DBUtil;
import com.zhiyou100.crm.util.DBUtil1;
import com.zhiyou100.crm.util.Role;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements RoleDao {
    DBUtil1 dbUtil1=new DBUtil1();
    ResultSet rs;
    @Override
    public List<Role> queryRole() {
        List<Role> roles=new ArrayList<>();
        String sql="select * from role";
        rs=dbUtil1.select(sql,null);
        try {
            while (rs.next()){
                int role_id = rs.getInt("role_id");
                String role_name = rs.getString("role_name");
                String role_desc = rs.getString("role_desc");
                String remark = rs.getString("remark");
                Timestamp update_time = rs.getTimestamp("update_time");
                Role role=new Role(role_id,role_name,role_desc,remark,update_time);
                roles.add(role);
            }
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }
        return roles;
    }

    @Override
    public int deleteRole(int id) {
        String sql="delete from role where role_id=?";
        Object[] obj={id};
        return dbUtil1.update(sql,obj);
    }

    @Override
    public int update(Role role) {
        String sql="update role set role_name=?,role_desc=?,update_time=? where role_id=?";
        Object[] obj={role.getRole_name(),role.getRole_desc(),role.getUpdate_time(),role.getRole_id()};
        return dbUtil1.update(sql,obj);
    }

    @Override
    public int insertRole(Role role) {
        String sql="insert into role(role_name,role_desc,update_time,create_time) values(?,?,?,?)";
        Object[] obj={role.getRole_name(),role.getRole_desc(),role.getUpdate_time(),role.getUpdate_time()};
        return dbUtil1.update(sql,obj);
    }
}
